package com.platform.common.secret.utils;

import cn.hutool.json.JSONUtil;
import com.platform.common.core.constant.CacheConstants;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.secret.utils.sm2.Sm2Util;
import com.platform.common.secret.utils.sm4.SM4Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author yzg
 * @date 2023/4/25
 * @apiNote 加解密工具类——根据加密工具自动加解密
 */
public class SecretEncryptUtil {
    protected static final Logger log = LoggerFactory.getLogger(SecretEncryptUtil.class);

    /**
     *  单纯加密
     * @param obj 加密数据
     * @return
     */
    public static String onlyEncode(Object obj,String secretCode,String privateSecret,String publicSecret) {

        String data = obj == null ? "{}" : JSONUtil.toJsonStr(obj);
        String result = JSONUtil.toJsonStr(data);
        try {
            switch (secretCode){
                case "SM2":{
                    //公钥加密
                    result= Sm2Util.encrypt(URLDecoder.decode(data.toString(), "UTF-8"),publicSecret);

                }; break;
                case "SM4":{
                    //对称加密——使用应用秘钥作为私钥
                    SM4Utils sm4Utils=new SM4Utils();
                    sm4Utils.secretKey=privateSecret;
                    result= sm4Utils.encryptData_ECB(URLDecoder.decode(data.toString(), "UTF-8"));

                };break;
                case "DES":{
                    DesUtil desUtil=new DesUtil(privateSecret);
                    result= desUtil.getEncString(URLDecoder.decode(data.toString(), "UTF-8"));
                }
                break;
                case "AES":{
                    result= AESECBUtil.Encrypt(URLDecoder.decode(data.toString(), "UTF-8"),privateSecret);
                };break;
                case "RSA":{
                    result= RSAUtils.encryptByPublicKey(URLDecoder.decode(data.toString(), "UTF-8"),publicSecret);

                };break;
                case "PBE":{
                    byte[] salt=publicSecret.getBytes(StandardCharsets.ISO_8859_1);
                    result=PBEUtil.encrypt(URLDecoder.decode(data.toString(), "UTF-8"),privateSecret,salt);
//                    byte[] encryptData=PBEUtil.encrypt(data.getBytes(),privateSecret,salt);
//                    result=new String(encryptData,StandardCharsets.ISO_8859_1);

                };break;
                default:
                    throw new RuntimeException("不支持指定加密工具");
            }
        }catch (Exception e){
            log.error("加密错误:{}", e);
        }
        if (result == null) {
            result = "";
        }
        try {
            return URLEncoder.encode(result,"UTF-8");
        } catch (Exception e) {
            log.error("URLEncoder编码错误:{}", data);
            return null;
        }
    }


    /**
     * 单纯解密
     * @param
     * @return
     */
    public static String onlyDecode(Object data,String secretCode,String privateSecret,String publicSecret) {
        //获取加密工具类
        String result = JSONUtil.toJsonStr(data);
        try {
            switch (secretCode){
                case "SM2":{
                    result= Sm2Util.decrypt(URLDecoder.decode(result.toString(), "UTF-8"),privateSecret);

                }; break;
                case "SM4":{
                    //对称加密——使用应用秘钥作为私钥
                    SM4Utils sm4Utils=new SM4Utils();
                    sm4Utils.secretKey=privateSecret;
                    result= sm4Utils.decryptData_ECB(URLDecoder.decode(result.toString(), "UTF-8"));

                };break;
                case "DES":{
                    DesUtil desUtil=new DesUtil(privateSecret);
                    result= desUtil.getDesString(URLDecoder.decode(result.toString(), "UTF-8"));
                }
                break;
                case "AES":{
                    result= AESECBUtil.Decrypt(URLDecoder.decode(result.toString(), "UTF-8"),privateSecret);
                };break;
                case "RSA":{
                    result= RSAUtils.decryptByPrivateKey(URLDecoder.decode(result.toString(), "UTF-8"),privateSecret);

                };break;
                case "PBE":{
                    byte[] salt=publicSecret.getBytes(StandardCharsets.ISO_8859_1);
                    result=PBEUtil.decrypt(URLDecoder.decode(result.toString(), "UTF-8"),privateSecret,salt);
//                    byte[] decryptData=PBEUtil.decrypt(result.getBytes(StandardCharsets.ISO_8859_1),privateSecret,salt);
//                    result=new String(decryptData);
                };break;
                default:
                    throw new RuntimeException("不支持指定加密工具");
            }
        }catch (Exception e){
            log.error("decrypt解码错误:{}", e);
        }
        if (result == null) {
            result = "";
        }
//        if(result.startsWith("[") && result.endsWith("]")){
//            //解密后是 集合
//            JSONArray jsonArray = JSONArray.parseArray(result) ;
//            List<T> list=jsonArray.toJavaList(beanClass);
//            return list;
//        }else  if(result.startsWith("{") && result.endsWith("}")){
//            //解密后是单个实体
//            T t = JSONUtil.toBean(result, beanClass);
//            return t;
//        }else {
//            log.error("decrypt解码错误:{}");
//            return null;
//        }
        try {
            return result;
        } catch (Exception e) {
            log.error("URLEncoder编码错误:{}", data);
            return null;
        }
    }

}
